package com.algo.conditional;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class MyConditional implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        Class needExistClass = metadata.getAnnotations().get(ConditionalOnExistClass.class).getClass("value");

        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();

        try {
            beanFactory.getBean(needExistClass);
            return true;
        } catch (NoSuchBeanDefinitionException e) {
            return false;
        }
    }
}
